<template>
  <div class="category">
    <!-- 头部区域 -->
    <div class="header">
      <!-- 新增按钮 -->
      <el-button type="primary" @click="addCategory">新增</el-button>
    </div>
    <!-- 表格内容展示区 -->
    <div class="table_content">
      <el-table :data="categoryData" style="width: 100%">
        <!-- 序号 -->
        <el-table-column type="index" label="序号" width="80"> </el-table-column>
        <!-- 名称 -->
        <el-table-column prop="name" label="名称" width="180"> </el-table-column>
        <!-- 排序号 -->
        <el-table-column prop="no" label="排序号" width="180"> </el-table-column>
        <!-- 描述 -->
        <el-table-column prop="description" label="描述"> </el-table-column>
        <!-- 操作 -->
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="editCategory(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button
              @click="deleteCategory(scope.row.id)"
              type="text"
              size="small"
              style="color: red"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      @close="dialogClose"
    >
      <!-- 表单区域 -->
      <!-- :model 表单对象 我们要提交的表单对象 -->
      <!-- :rules 表单验证的规则 -->
      <!-- :ref 最终通过ref找到这张表单 -->
      <el-form
        :model="categoryForm"
        :rules="rules"
        ref="category_form"
        label-width="80px"
      >
        <!-- 产品名称 -->
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="categoryForm.name"></el-input>
        </el-form-item>
        <!-- 产品名称 -->
        <el-form-item label="序号" prop="no">
          <el-input v-model="categoryForm.no"></el-input>
        </el-form-item>
        <!-- 产品描述 -->
        <el-form-item label="栏目介绍" prop="description">
          <el-input
            type="textarea"
            v-model="categoryForm.description"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部操作按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <!-- 提交表单后重置表单 -->
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 引入axios
import { get, post } from "@/utils/request";
export default {
  data() {
    return {
      // 栏目数据源
      categoryData: [],
      
      // 控制模态框的显示与隐藏
      dialogVisible: false,
      dialogTitle: "新增栏目信息",
      // 表单对象
      categoryForm: {
        name: "",
        no: "",
        description: "",
      },
      // 表单验证规则
      rules: {
        // 栏目名称
        name: [{ required: true, message: "请输入栏目名称", trigger: "blur" }],
      },
    };
  },
  computed: {},
  methods: {
    // 分页获取所有栏目的数据
    async getAllCategoryData() {
      let res = await get("/category/findAll");
      this.categoryData = res.data;
    },
    // 新增栏目
    addCategory() {
      // 清空表单对象
      this.categoryForm = {};
      // 设置模态框的标题
      this.dialogTitle = "新增栏目信息";
      // 模态框显示
      this.dialogVisible = true;
    },
    // 编辑栏目
    editCategory(row) {
      // 将当前点击的这一行的对象 赋值到表单框
      this.categoryForm = { ...row };
      this.dialogTitle = "编辑栏目信息";
      // 模态框显示
      this.dialogVisible = true;
    },
    // 表单提交
    submitForm() {
      this.$refs["category_form"].validate(async (valid) => {
        if (valid) {
          // 发送表单验证的请求 提交表单对象
          let res = await post("/category/saveOrUpdate", this.categoryForm);
          // 给用户一个成功的提示
          if (res.status == 200) {
            this.$message({
              message: res.message,
              type: "success",
            });
            // 重新刷新页面
            this.getAllCategoryData();
            // 关闭模态框
            this.dialogClose();
          } else {
            this.$message.error(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 关闭模态框 调用表单重置
    dialogClose() {
      this.$refs["category_form"].resetFields();
      //关闭模态框
      setTimeout(() => {
        this.dialogVisible = false;
      }, 200);
    },
    // 删除栏目
     deleteCategory(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await get("/category/deleteById", { id });
          this.$message({
            type: "success",
            message:res.message,
          });
          this.getAllCategoryData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    // 自动获取栏目数据
    this.getAllCategoryData();
  },
  mounted() {},
};
</script>
<style scoped>
</style>